
/**author:yebinx email:yebinx@21cm.com*/
export default class MyDialogManager{
    private static _ins:MyDialogManager;
    private particleChcae:any={};
	private prefatCache:any={};
	private animationCache:any={};
    constructor(){

    }

    public sceneOpen(_url:string,_closeOther:boolean=false,_param?:any,_complete?:Laya.Handler,_progress?:Laya.Handler){
        Laya.Scene.open(_url,_closeOther,_param,_complete,_progress);
    }
	
	public closeScene(url:string, name:string = ""){
		 return Laya.Scene.close(url,name);
	}
	
	public isOnStage(url:string, name:string = "") {
	    var list = Laya.Scene.unDestroyedScenes;
	    for (var i = 0, n = list.length; i < n; i++) {
	        var scene = list[i];
	        if (scene && scene.parent && scene.url === url && scene.name == name) {
	            return  true;
	        }
	    }
	    return false;
	}

    public static getIns():MyDialogManager{
        if(!this._ins)this._ins=new MyDialogManager();
        return this._ins;
    }

    public loadParticle(_url:string,_complete?:Function){
        if(this.particleChcae[_url])_complete(this.particleChcae[_url]);
        else{
            Laya.loader.load(_url, new Laya.Handler(this, (settings)=>{
                        // 创建 Particle2D 实例
                        //console.log(Laya.Particle2D)
                    
                    let partIns = new Laya.Particle2D(settings);
                    this.particleChcae[_url]=partIns;
                    _complete(this.particleChcae[_url]);
            }), null, Laya.Loader.JSON);
        }
    }

    public loadPrefat(_url:string,_complete?:Function){
        if(this.prefatCache[_url])_complete(this.prefatCache[_url]);
        else{
            Laya.loader.create(_url, new Laya.Handler(this, (_prefab:Laya.Prefab)=>{
                        this.particleChcae[_url]=_prefab;
                        _complete(_prefab);
                }));
            }
    }

    public loadAnimation(_url:string,_complete?:Function){
        if(this.animationCache[_url])_complete(this.animationCache[_url])
        else{
            Laya.loader.load(_url, new Laya.Handler(this, ()=>{
                    let Animation = Laya.Animation;
                    let ani = new Animation();
                    ani.loadAtlas(_url);
                    this.animationCache[_url] = ani
                    _complete(ani)
            }), null, Laya.Loader.ATLAS);
        }
    }
	
	 public  getNode(_node:Laya.Node,_path:string):any{
	        let tnames = _path.split("/");
	        let tnode=_node.getChildByName(tnames[0]);
	        for(let i=1;i<tnames.length;i++){
	            tnode=tnode.getChildByName(tnames[i]);
	            if(!tnode)return null;
	        }
	        return tnode;
	}
}